<?php

include_once($_SERVER['DOCUMENT_ROOT'] . '/mba/models/MBA.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/mba/PHPExcel_1.7.9_doc/Classes/PHPExcel.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/mba/PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // bat su kien button them don vi
    if (isset($_POST['btnthemmba'])) {
        $son0Err = $congsuatErr = "";
        // kiem tra so n0
        if (empty($_POST["txtson0"])) {
            $son0Err = "Số N0 không thể bỏ trống";
        } else {
            $son0 = test_input($_POST["txtson0"]);
            // kiem tra so n0 nhap vao phai la so
            if (preg_match("/^[0-9]*$/", $son0) == false) {
                $son0Err = "Số N0 phải là số";
            }
        }
        // kiem tra cong suat
        if (empty($_POST["txtcongsuat"])) {
            $congsuatErr = "Công suất không thể bỏ trống";
        } else {
            $congsuat = test_input($_POST["txtcongsuat"]);
            // kiem tra so n0 nhap vao phai la so
            if (preg_match("/^[0-9]*$/", $congsuat) == false) {
                $congsuatErr = "Công suất phải là số";
            }
        }
        if ($congsuatErr == "" && $son0Err == "") {

            // LAY CAC GIA TRI TU FORM THEM MBA
            $mba = new MBA();
            $son0 = $_POST['txtson0'];
            $congsuat = $_POST['txtcongsuat'];
            $maloaidau = $_POST['txtmaloaidau'];
            $madonvi = $_POST['txtmadonvi'];
            $ngaychuyenden = $_POST['txtngaychuyenden'];
            $madienap = $_POST['txtmadienap'];
            $manhasx = $_POST['txtmanhasx'];
            $maloaimay = $_POST['txtmaloaimay'];
            $nuocsx = $_POST['txtnuocsx'];

            $msts = $_POST['txtmsts'];
            $matram = $_POST['txtmatram'];
            $tinhtrangmay = $_POST['txttinhtrangmay'];
            $ngaycotinhtrang = $_POST['txtngaycotinhtrang'];
            $diengiaitt = $_POST['txtdiengiaitt'];
            $ghichu = $_POST['txtghichu'];
            $ngayvanhanh = $_POST['txtngayvanhanh'];
            $ngayngungvanhanh = $_POST['txtngayngungvanhanh'];
            $ngaydaitu = $_POST['txtngaydaitu'];
            $nhanxet = $_POST['txtnhanxet'];


            $chieudai = $_POST['txtchieudai'];
            $chieurong = $_POST['txtchieurong'];
            $chieucao = $_POST['txtchieucao'];
            $namsx = $_POST['txtnamsx'];
            $ruotmay = $_POST['txtruotmay'];
            $daumay = $_POST['txtdaumay'];
            $thongsodo = $_POST['txtthongsodo'];
            $namnhapve = $_POST['txtnamnhapve'];


            // gan cac thuoc tinh cho MBA
            $mba->setSO_N0($son0);
            $mba->setCONG_SUAT($congsuat);
            $mba->setMA_LOAI_DAU($maloaidau);
            $mba->setMA_DON_VI($madonvi);
            $mba->setNGAY_CHUYEN_DEN($ngaychuyenden);
            $mba->setMA_DIEN_AP($madienap);
            $mba->setMA_NSX($manhasx);
            $mba->setMA_LOAI($maloaimay);
            $mba->setTEN_NUOC($nuocsx);

            $mba->setMSTS($msts);
            $mba->setMA_TRAM($matram);
            $mba->setTEN_TINH_TRANG_MAY($tinhtrangmay);
            $mba->setTINH_TRANG_NGAY($ngaycotinhtrang);
            $mba->setDG_TINH_TRANG($diengiaitt);
            $mba->setGHI_CHU($ghichu);
            $mba->setNGAY_NGUNG_VH($ngayngungvanhanh);
            $mba->setNGAY_VAN_HANH($ngayvanhanh);
            $mba->setNGAY_DAI_TU($ngaydaitu);
            $mba->setNHAN_XET($nhanxet);


            $mba->setCHIEU_DAI($chieudai);
            $mba->setCHIEU_RONG($chieurong);
            $mba->setCHIEU_CAO($chieucao);
            $mba->setDAU_MAY($daumay);
            $mba->setRUOT_MAY($ruotmay);
            $mba->setTHONG_SO_DO($thongsodo);
            $mba->setTONG($TONG);
            $mba->setNAM_SX($namsx);
            $mba->setNAM_NHAP_VE($namnhapve);

            // them vao csdl
            if ($mba->isExits("may_bien_ap", "SO_N0='" . $mba->getSO_N0() . "'") <= 0) {
                if ($mba->themMBA() > 0) {
                    echo "<script language=javascript> alert('Đã thêm thành công!'); </script>";
                } else {
                    echo "<script language=javascript> alert('Không thêm được!'); </script>";
                }
            } else {
                echo "<script language=javascript> alert('So N0 đã tồn tại ! Không thêm được !'); </script>";
            }
        }
    }

    // bat su kien button sua
    if (isset($_POST['btnsuadv'])) {
        if ($donvi->suaDV() > 0) {
            echo "<script language=javascript> alert('Da sua thanh cong!'); </script>";
            // co gang de xoa cac thong tin ve don vi da sua ?????
            $donvi = new DonVi();
        } else {
            echo "<script language=javascript> alert('Không sửa được!'); </script>";
            // co gang de xoa cac thong tin ve don vi da sua ?????
            $donvi = new DonVi();
        }
    }

    // bat su kien button xoa
    if (isset($_POST['btnxoadv'])) {
        if ($donvi->xoaDV()) {
            echo "<script language=javascript> alert('Da xoa thanh cong!'); </script>";
            // co gang de xoa cac thong tin ve don vi da xoa ?????
            $donvi = new DonVi();
        } else {
            echo "<script language=javascript> alert('Không xóa được!'); </script>";
            require($_SERVER['DOCUMENT_ROOT'] . '/QL MBA/Views/CapNhatDonVi.php');
        }
    }
    // khi nhan nut upload



    if (isset($_POST['upload'])) {
        if ($_FILES["file"]["name"] != "") {

            $objPHPExcel = PHPExcel_IOFactory::load($_FILES["file"]["tmp_name"]);
            foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
                $worksheetTitle = $worksheet->getTitle();
                $highestRow = $worksheet->getHighestRow(); // e.g. 10
                $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
                $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
            }

            //
            $nrColumns = ord($highestColumn) - 64;


            //
            echo "File " . $worksheetTitle . " has ";
            echo $nrColumns . ' columns';
            echo ' y ' . $highestRow . ' rows.';
            //
            echo 'Data: <table width="100%" cellpadding="3" cellspacing="0"><tr>';
            for ($row = 1; $row <= $highestRow; ++$row) {
                echo '<tr>';
                for ($col = 0; $col < $highestColumnIndex; ++$col) {
                    $cell = $worksheet->getCellByColumnAndRow($col, $row);
                    $val = $cell->getValue();
                    if ($row === 1)
                        echo '<td style="background:#000; color:#fff;">' . $val . '</td>';
                    else
                        echo '<td>' . $val . '</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
// quang vao csdl
            for ($row = 2; $row <= $highestRow; ++$row) {
                $val = array();
                for ($col = -1; $col < $highestColumnIndex; $col++) {
                    $cell = $worksheet->getCellByColumnAndRow($col, $row);
                    $val[] = $cell->getValue();
                }

                // $sql="insert into tablename(column1, column2, column3, column4, column5, column6)
                // values('".$val[1] . "','" . $val[2] . "','" . $val[3]. "','" . $val[4]. "','" . $val[5]. "','" . $val[6]. "')";
//Run your mysql_query
                $donvi = new DonVi();
                $madv = $val[1];
                $tendv = $val[2];
                $diachidv = $val[3];
                $lienhedv = $val[4];
                $tentk = $val[1];
                $matkhau = $val[1];
                $quyen = 1;


                $donvi->setMaDV($madv);
                $donvi->setTenDV($tendv);
                $donvi->setMaDV($madv);
                $donvi->setTenDV($tendv);
                $donvi->setDiaChiDV($diachidv);
                $donvi->setLienHeDV($lienhedv);
                $donvi->setTenTK($tentk);
                $donvi->setMatKhau($matkhau);
                $donvi->setQuyen($quyen);

                if ($donvi->isExits("don_vi", "MA_DON_VI='" . $donvi->getMaDV() . "'") <= 0) {
                    if ($donvi->themDV() > 0) {
                        echo "<td class='level_1_$ci' align='center'>OK</td></tr>";
                    } else {
                        echo "<td class='level_1_$ci' align='center'>bị trùng</td></tr>";
                    }
                } else {
                    //khoa da ton tai
                    echo "<td class='level_1_$ci' align='center'>bị trùng</td></tr>";
                }
            }
        } else {
            echo "Chon file excel de upload!";
        }
    }
}

// cat bo khoang trong thua( ' ' ,'tab','new line'), dau '/'
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>
